#include "CircularLinkList.h"
#include <gtest/gtest.h>

TEST(CircularLinkListTest, InitiallyEmpty) {
    CircularLinkList list;
    EXPECT_TRUE(list.Empty());
    EXPECT_EQ(list.Size(), 0);
}

TEST(CircularLinkListTest, PushFront) {
    CircularLinkList list;
    list.PushFront(10);
    list.PushFront(20);
    list.PushFront(30);
    EXPECT_EQ(list.Size(), 3);
    EXPECT_EQ(list.Get(1), 30);
    EXPECT_EQ(list.Get(2), 20);
    EXPECT_EQ(list.Get(3), 10);
}

TEST(CircularLinkListTest, PushBack) {
    CircularLinkList list;
    list.PushBack(1);
    list.PushBack(2);
    list.PushBack(3);
    list.PushBack(4);
    EXPECT_EQ(list.Size(), 4);
    EXPECT_EQ(list.Get(1), 1);
    EXPECT_EQ(list.Get(4), 4);
}

TEST(CircularLinkListTest, PopFront) {
    CircularLinkList list;
    list.PushBack(100);
    list.PushBack(200);
    list.PushBack(300);
    list.PopFront();
    EXPECT_EQ(list.Size(), 2);
    EXPECT_EQ(list.Get(1), 200);
}

TEST(CircularLinkListTest, PopBack) {
    CircularLinkList list;
    list.PushBack(5);
    list.PushBack(6);
    list.PushBack(7);
    list.PopBack();
    EXPECT_EQ(list.Size(), 2);
    EXPECT_EQ(list.Get(2), 6);
}

TEST(CircularLinkListTest, Insert) {
    CircularLinkList list;
    list.PushBack(1);
    list.PushBack(2);
    list.PushBack(4);
    list.Insert(3, 3);  // 插入到末尾前
    EXPECT_EQ(list.Get(3), 3);
    list.Insert(1, 0);  // 插入到开头
    EXPECT_EQ(list.Get(1), 0);
    EXPECT_EQ(list.Size(), 5);
}

TEST(CircularLinkListTest, Remove) {
    CircularLinkList list;
    list.PushBack(1);
    list.PushBack(2);
    list.PushBack(2);
    list.PushBack(3);
    list.Remove(2);
    EXPECT_EQ(list.Size(), 3);
    EXPECT_EQ(list.Get(2), 2);  // 第二个 2 成为新第 2
}

TEST(CircularLinkListTest, RemoveAll) {
    CircularLinkList list;
    list.PushBack(2);
    list.PushBack(2);
    list.PushBack(2);
    list.PushBack(3);
    list.PushBack(2);
    list.RemoveAll(2);
    EXPECT_EQ(list.Size(), 1);
    EXPECT_EQ(list.Get(1), 3);
}

TEST(CircularLinkListTest, SetAndGet) {
    CircularLinkList list;
    for (int i = 1; i <= 10; ++i)
        list.PushBack(i);
    list.Set(5, 100);
    EXPECT_EQ(list.Get(5), 100);
}

TEST(CircularLinkListTest, Find) {
    CircularLinkList list;
    list.PushBack(11);
    list.PushBack(22);
    list.PushBack(33);
    EXPECT_TRUE(list.Find(22));
    EXPECT_FALSE(list.Find(99));
}

TEST(CircularLinkListTest, Clear) {
    CircularLinkList list;
    for (int i = 0; i < 100; ++i)
        list.PushBack(i);
    EXPECT_EQ(list.Size(), 100);
    list.Clear();
    EXPECT_TRUE(list.Empty());
    EXPECT_EQ(list.Size(), 0);
}

TEST(CircularLinkListTest, ComplexOperations) {
    CircularLinkList list;
    for (int i = 1; i <= 50; ++i)
        list.PushBack(i);
    for (int i = 1; i <= 10; ++i)
        list.PopFront();
    for (int i = 0; i < 10; ++i)
        list.PushFront(i);
    list.Remove(30);
    list.RemoveAll(1); // 可能多次出现
    EXPECT_GE(list.Size(), 0);
}

int main() {
    ::testing::InitGoogleTest();
    return RUN_ALL_TESTS();
}